package display;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import structs.database.Database;
import Util.Util;
import display.tabs.AdoptionTab;
import display.tabs.BookInfoTab;
import display.tabs.InventoryQueryTab;
import display.tabs.SettingsTab;
import display.tabs.ThreadMonitor;
import display.tabs.ToDoTab;

public class Main {

	private JFrame contentFrame;
	private JTabbedPane contentPane;
	
	private InventoryQueryTab invQ;
	//private InventoryUpdate invU;
	private InventoryData inv;
	private BookInfoTab bInfo;
	//private CourseInfo cInfo;
	private AdoptionTab aCon;
	private SettingsTab set;
	//private JComboBox termBox;
	private JPanel panel;

	private static final String dbURL = "jdbc:derby://localhost:1527/inventory;create=true;user=me;password=mine";
	public static final String books = "books", pubs = "publishers",
			orders = "orders", courses = "courses", adoptions = "adoptions",
			quantities = "quantities";
	// jdbc Connection
	private static Database db = null;
	private MainActionListener mal = new MainActionListener();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
					Main window = new Main();
					window.contentFrame.setVisible(true);
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		
		initInv();
		
		Thread dbInit = new Thread("DB Initialization") {
			@Override
			public void run() {
				try {
					db = new Database(dbURL);
					inv.setDB(db);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};

		//long t0 = System.currentTimeMillis();
		dbInit.start();
		
		initGUI();
		
		try {
			dbInit.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println("Starting time: "+ (System.currentTimeMillis() - t0));
		long t1 = System.currentTimeMillis();
		initTerms();
		Util.printStatusString("initTerms time: "+ (System.currentTimeMillis() - t1));
		
	}
	
	private void initTerms() {
		set.initTerms();
	}
	
	private void initGUI() {
		long t0 = System.currentTimeMillis();
		initDisplay();
		initTabs();
		Util.printStatusString("initGUI time: "+(System.currentTimeMillis()-t0));
	}
	
	private void initInv() {
		inv = new InventoryData();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initDisplay() {
		contentFrame = new JFrame();
		contentFrame.setTitle("Students First");
		contentFrame.setBounds(100, 100, 480, 286);
		contentFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
		contentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 0.0, 0.0, 0.0,
				1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 0.0, 1.0, 0.0, 1.0, 0.0,
				0.0, 0.0, 1.0, Double.MIN_VALUE };
		contentFrame.setContentPane(contentPane = new JTabbedPane());
		contentPane.setSize(new Dimension(32767, 32767));
		contentPane.setPreferredSize(new Dimension(32767, 32767));
		//.getContentPane().setLayout(gridBagLayout);
		
		contentFrame.addWindowListener(mal);
	}

	private void initTabs() {
		
		contentPane.addTab("Sales", new ToDoTab());
		contentPane.addTab("Inventory", invQ = new InventoryQueryTab(inv));
		contentPane.addTab("Courses", aCon = new AdoptionTab(inv));
		contentPane.addTab("Books", bInfo = new BookInfoTab(inv));
		contentPane.addTab("Purchase Orders", new ToDoTab());
		contentPane.addTab("Settings", set = new SettingsTab(inv));
		contentPane.addTab("Threads", new ThreadMonitor());
	}

	private void save() {
		long t0 = System.currentTimeMillis();
		inv.save();
		System.out.println("closing time: "+(System.currentTimeMillis()-t0));
	}
	
	private class MainActionListener implements ActionListener, MouseListener, WindowListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			/*Util.printStatusString("action performed");
			String cmd = arg0.getActionCommand();
			if (cmd.equalsIgnoreCase("iq")) {
				invQ = new InventoryQueryPane(inv); invQ.setVisible(true);}
			else if (cmd.equalsIgnoreCase("iu")) {
				invU = new InventoryUpdate(); invU.setVisible(true);}
			else if (cmd.equalsIgnoreCase("bi")) {
				bInfo = new BookInfoPane(inv); bInfo.setVisible(true);}
			else if (cmd.equalsIgnoreCase("ci")) {
				cInfo = new CourseInfo(inv.getCurTerm()); cInfo.setVisible(true);}
			else if (cmd.equalsIgnoreCase("ac")) {
				aCon = new AdoptionPane(inv); aCon.setVisible(true);}
			else if (cmd.equalsIgnoreCase("tb")) {
				Util.printStatusString("term box!");
				String termString = termBox.getSelectedItem().toString();
				if (termString.equalsIgnoreCase("New Term"))
					new NewTermDialog(inv,db).setVisible(true);
				else
					inv.selectTerm(Term.parse(termString));
			}*/
			
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			/*if (arg0.getSource().equals(panel)) {
				DatabaseDisplay dd = new DatabaseDisplay(db);
				dd.setVisible(true);
			}
			else if (arg0.getSource().equals("tb")) {
				
			}*/
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowActivated(WindowEvent arg0) {
			// TODO Auto-generated method stub
			//termBox.setModel(new DefaultComboBoxModel(inv.getTerms().toArray()));
			//termBox.addItem("New Term");
		}

		@Override
		public void windowClosed(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowClosing(WindowEvent arg0) {
			// TODO Auto-generated method stub
			save();
		}

		@Override
		public void windowDeactivated(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowDeiconified(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowIconified(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowOpened(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
